package cpczxt;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

public abstract class JsonUtils {
	private static Gson gson = new GsonBuilder().create();

	public static Gson getGson() {
		return gson;
	}

	public static String getJson(Object value) {
		return gson.toJson(value);
	}

	public static <T> T getValue(String json, Class<T> clazz) {
		return gson.fromJson(json, clazz);
	}

	public static <T> List<T> getList(String json, Class<T> clazz) {
		List<JsonObject> objects = gson.fromJson(json, new TypeToken<List<JsonObject>>() {
		}.getType());
		List<T> list = new ArrayList<T>();
		for (JsonObject object : objects) {
			list.add(gson.fromJson(object, clazz));
		}
		return list;
	}

	public static Map<String, Object> getMap(String json) {
		return gson.fromJson(json, new TypeToken<Map<String, Object>>() {
		}.getType());
	}
}